﻿using APIShoppingSystem.GetSession;
using APIShoppingSystem.Model;
using IShoppingBll;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ShoppingEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace APIShoppingSystem.Controllers
{
	[Route("api/[controller]/[action]")]
	[ApiController]
	public class ShoppingCartController : ControllerBase
	{
		private IShoppingCartBll _shoppingCartBll;
		private ICommodityInfoBll _commodityInfoBll;

		public ShoppingCartController(IShoppingCartBll shoppingCartBll, ICommodityInfoBll commodityInfoBll)
		{
			_shoppingCartBll = shoppingCartBll;
			_commodityInfoBll = commodityInfoBll;
		}

        /// <summary>
        /// 购物车页面分页展示、模糊查询
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="CommodityName"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShoppingCartPageList(int page, int limit, string goodsId)
        {
            int count;
            UserInfo userInfo = this.GetSessionUserInfo();
            object shoppingCarts = _shoppingCartBll.GetShoppingCartPageList(page, limit, out count, goodsId, userInfo);

            return new JsonResult(new
            {
                code = 0,
                msg = "成功",
                count = count,
                data = shoppingCarts
            });
        }

        /// <summary>
        /// 单个删除购物车商品
        /// </summary>
        /// <param name="categoryId"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult DeleteShoppingCart([FromForm]string ShoppingCartId)
        {
            CustomActionResult res = new CustomActionResult();

            if (_shoppingCartBll.Delete(ShoppingCartId))
            {
                res.Status = 1;
                res.Msg = "成功";
                res.IsSuccess = true;
            }
            return new JsonResult(res);
        }

        /// <summary>
        /// 购物车多个商品删除
        /// </summary>
        /// <param name="ShoppingCartId"></param>
        /// <returns></returns>
       [HttpPost]
        public IActionResult DeletesShoppingCarts([FromForm] string[] ShoppingCartId)
        {
            CustomActionResult res = new CustomActionResult();
            return new JsonResult(res);
        }

        /// <summary>
        /// 获取加入购物车商品信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult AddShoppingCartOption(string CommodityId)
        {
            CustomActionResult res = new CustomActionResult();
            //获取商品信息
            var commodityInfo = _commodityInfoBll.GetShoppingCartOption(CommodityId);
            res.IsSuccess = true;
            res.Status = 1;
            res.Msg = "成功";
            res.Datas = commodityInfo;
            return new JsonResult(res);
        }

        /// <summary>
        /// 将商品加入购物车
        /// </summary>
        /// <param name="CommodityId"></param>
        /// <param name="Num"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddShoppingCart([FromForm] string CommodityId, [FromForm] int Num)
        {
            CustomActionResult res = new CustomActionResult();

            if (Num < 0)
            {
                res.Msg = "数量错误,加入购物车失败";
                return new JsonResult(res);
            }

            UserInfo userInfo = this.GetSessionUserInfo();
            ShoppingCart shoppingCart = new ShoppingCart()
            {
                ID = Guid.NewGuid().ToString(),
                GoodsId = CommodityId,
                UserId = userInfo.ID,
                Num = Num,
                AddTime = DateTime.Now
            };

            if (_shoppingCartBll.Add(shoppingCart))
            {
                res.Status = 1;
                res.Msg = "加入购物车成功";
                res.IsSuccess = true;
            }
            return new JsonResult(res);
        }

        /// <summary>
        /// 编辑购物车数量视图赋值
        /// </summary>
        /// <param name="ShoppingCart"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult UpdateShoppingCartSelect(string ShoppingCartId)
        {
            CustomActionResult res = new CustomActionResult();
            //获取购物车商品信息
            var shoppingCart = _shoppingCartBll.GetShoppingCartOption(ShoppingCartId);
            res.IsSuccess = true;
            res.Status = 1;
            res.Msg = "成功";
            res.Datas = shoppingCart;
            return new JsonResult(res);
        }

        /// <summary>
        /// 修改购物车商品数量
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdateShoppingCart([FromForm] string ShoppingCartId, [FromForm] int Num)
        {
            CustomActionResult res = new CustomActionResult();

            if (Num < 0)
            {
                res.Msg = "您输入的数量错误";
                return new JsonResult(res);
            }


            ShoppingCart shoppingCart = _shoppingCartBll.GetEntity(ShoppingCartId);
            shoppingCart.Num = Num;

            bool isSuccess = _shoppingCartBll.UpdateEntity(shoppingCart);
            if (isSuccess)
            {
                res.Status = 1;
                res.IsSuccess = true;
                res.Msg = "成功";
            }
            return new JsonResult(res);
        }

        /// <summary>
        /// 单件商品购买
        /// </summary>
        /// <param name="ShoppingCartId"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PlayShoppingCart([FromForm] string ShoppingCartId)
        {
            CustomActionResult res = new CustomActionResult();
            string msg;
            UserInfo userInfo = this.GetSessionUserInfo();
            bool isSuccess = _shoppingCartBll.PlayShoppingCart(ShoppingCartId, userInfo, out msg);
            if (isSuccess)
            {
                res.IsSuccess = true;
                res.Status = 1;
                res.Msg = msg;
            }
            else
            {
                res.Msg = msg;
            }


            return new JsonResult(res);

        }

        /// <summary>
        /// 多件商品购买
        /// </summary>
        /// <param name="ShoppingCartId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult AllPlayShoppingCart([FromForm] string[] ShoppingCartId)
        {
            CustomActionResult res = new CustomActionResult();
            string msg;
            UserInfo userInfo = this.GetSessionUserInfo();


            bool isSuccess = _shoppingCartBll.PlayAllShoppingCart(ShoppingCartId, userInfo, out msg);
            if (isSuccess)
            {
                res.IsSuccess = true;
                res.Status = 1;
                res.Msg = msg;
            }
            else
            {
                res.Msg = msg;
            }
            return new JsonResult(res);
        }

    }
}
